import axios from "axios";
import getCurrentTimestamp from "@/public/methods.js";

const api = 'http://nbxt.oa.x-lf.cn'

/**
 * 登录
 * @param data (username,password)
 * @returns {*}
 */
const login = (data) => {
    return axios({
        url: api+"/auth/login",
        method: "post",
        data: data,
        headers: {
            'content-type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        },
    })
}

/**
 * 注册
 * @param data (username,password,address,phone,email,sex,age)
 * @returns {*}
 */
const register = (data) => {
    return axios({
        url: api + "/auth/register",
        method: "post",
        data: data,
        headers: {
            'content-type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 退出登录
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const logout = (token) => {
    return axios({
        url: api + "/auth/logout",
        method: "get",
        headers: {
            'Authorization':'Bearer '+token,
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 修改密码
 * @param data (oldPassword,newPassword,confirmPassword)
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const changePassword = (data, token) => {
    return axios({
        url: api + "/auth/password",
        method: "put",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 忘记密码
 * @param data (email,check（邮箱验证码）,newPassword)
 * @returns {*}
 */
const forgetPassword = (data) => {
    return axios({
        url: api + "/auth/password/forget",
        method: "put",
        data: data,
        headers: {
            'content-type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 邮箱登录
 * @param params 对象（包含邮箱email：string，验证码code：integer）
 * @returns {*}
 */
const authLoginWithEmail = (params) => {
    return axios({
        url: api + "/auth/login/email",
        method: "get",
        params: params,
        headers: {
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 发送邮箱验证码
 * @param email 邮箱（string）
 * @returns {*}
 */
const authSendEmailCode = (email) => {
    return axios({
        url: api + "/auth/email/code",
        method: "get",
        params: email,
        headers: {
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 获取全部用户信息
 * @param data （page，limit，search，role）
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const userAllCurrent = (data, token) => {
    return axios({
        url: api + "/user/current/all",
        method: "post",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 获取一个用户信息（敏感）
 * @param params (id,username,email,phone)可以只传一个参数
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const userCurrent = (params, token) => {
    return axios({
        url: api + "/user/current",
        method: "get",
        params: params,
        headers: {
            'Authorization':'Bearer '+token,
            // 'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 新增用户
 * @param data (username,password,address,phone,email,sex,age)必须全部
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const userAdd = (data, token) => {
    return axios({
        url: api + "/user/add",
        method: "post",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 编辑用户
 * @param data (id,address,phone,email,age,signature,sex,avatar,nickname,description
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const userEdit = (data, token) => {
    return axios({
        url: api + "/user/edit",
        method: "put",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 删除用户
 * @param id
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const userDelete = (id, token) => {
    return axios({
        url: api + "/user/delete",
        method: "put",
        params: {
            id: id
        },
        headers: {
            'Authorization':'Bearer '+token,
            'content-type':'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 锁定用户
 * @param data （id，isLock(1解锁）)
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const userLock = (data, token) => {
    return axios({
        url: api + "/user/lock",
        method: "put",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 获取用户信息（非敏感）
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const userGetProfile = (token) => {
    return axios({
        url: api + "/user/profile/get",
        method: "get",
        headers: {
            'Authorization':'Bearer '+token,
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 修改用户信息（普通用户）
 * @param data (id,username,address,phone,email,sex,age,signature,avatar,nickname,description)
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const userEditProfile = (data, token) => {
    return axios({
        url: api + "/user/profile/edit",
        method: "put",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 查询角色
 * @param id 可选，不填则是查询所有？
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const roleGet = (id, token) => {
    return axios({
        url: api + "/role/get",
        method: "get",
        params: id,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 新增角色
 * @param data (name,displayName)示例(name:teacher, displayName:老师)必须
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const roleAdd = (data, token) => {
    return axios({
        url: api + "/role/add",
        method: "post",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 编辑角色
 * @param data (id,name,displayName（中文）)必须
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const roleEdit = (data, token) => {
    return axios({
        url: api + "/role/edit",
        method: "put",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 删除角色
 * @param id
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const roleDelete = (id, token) => {
    return axios({
        url: api + "/role/delete",
        method: "delete",
        params: id,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 给角色添加用户
 * @param data （rid，uid）
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const roleAddUser = (data, token) => {
    return axios({
        url: api + "/role/user/add",
        method: "post",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 修改用户的角色
 * @param data （uid，rid）
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const roleChangeUser = (data, token) => {
    return axios({
        url: api + "/role/user/change",
        method: "put",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 移除用户的角色
 * @param uid
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const roleRemoveUser = (uid, token) => {
    return axios({
        url: api + "/role/user/remove",
        method: "delete",
        data: uid,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 查询轮播图（管理员）
 * @param id 可选
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const infoGetHeaderImage = (id, token) => {
    return axios({
        url: api + "/info/header-image/get",
        method: "get",
        params: id,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 编辑轮播图信息
 * @param id 轮播图id
 * @param data (displayOrder（排序顺序）,image（图片地址）,title,description,isActive（是否启用）)
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const infoEditHeaderImage = (id, data, token) => {
    return axios({
        url: api + "/info/header-image/edit",
        method: "put",
        params: id,
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 新增轮播图
 * @param data (displayOrder,image,title,description,isActive)
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const infoAddHeaderImage = (data, token) => {
    return axios({
        url: api + "/info/header-image/add",
        method: "post",
        data: data,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 删除轮播图
 * @param id
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const infoDeleteHeaderImage = (id, token) => {
    return axios({
        url: api + "/info/header-image/del",
        method: "delete",
        params: id,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}

/**
 * 展示轮播图，顺序或倒序
 * @param showType 0倒序，1顺序
 * @param token
 * @returns {Promise<AxiosResponse<any>> | *}
 */
const infoEditSettingHeaderImage = (showType, token) => {
    return axios({
        url: api + "/info/header-image/edit-setting",
        method: "put",
        data: showType,
        headers: {
            'Authorization':'Bearer '+token,
            'Content-Type': 'application/json;charset=utf-8',
            'Timestamp': getCurrentTimestamp()
        }
    })
}


export default {
    login,
    register,
    logout,
    changePassword,
    forgetPassword,
    authLoginWithEmail,
    authSendEmailCode,

    userAllCurrent,
    userCurrent,
    userAdd,
    userEdit,
    userDelete,
    userLock,
    userGetProfile,
    userEditProfile,

    roleGet,
    roleEdit,
    roleAdd,
    roleDelete,
    roleAddUser,
    roleChangeUser,
    roleRemoveUser,

    infoGetHeaderImage,
    infoEditHeaderImage,
    infoAddHeaderImage,
    infoDeleteHeaderImage,
    infoEditSettingHeaderImage,


}